package
{
	import laya.display.Graphics;
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.filters.GlowFilter;
	import laya.net.LoaderManager;
	import laya.resource.Texture;
	import laya.ui.Image;
	import laya.utils.Handler;
	import laya.utils.Tween;

	public class BitmapSample
	{
		private var monkey0:String = "res/monkey0.png"
		private var monkey1:String = "res/monkey1.png"
		private var monkey2:String = "res/monkey2.png"

		private var spImg:Sprite;
		private var _flag:Boolean;

		private var img:Image;

		private var textureImg:Sprite;
		
		private var _shpMask:Sprite;
		
		public function BitmapSample()
		{
			/*img = new Image(); //异步加载图片
			img.pos(10,20);
			img.skin = monkey0;
			Laya.stage.addChild(img);
			
			spImg = new Sprite();
			spImg.loadImage(monkey0);
			spImg.pos(100,100);
			
			Laya.stage.addChild(spImg);
			spImg.on(Event.CLICK,this,switchImg);*/
			 
			var radius:int = 20;
			_shpMask = new Sprite();
			_shpMask.graphics.drawCircle(0,0,radius,"#ff0000");						
			//_shpMask.pos(radius*2,radius*2);
			
			//drawTexture()文理绘制图片 必须先加载再调用
			textureImg = new Sprite();
			Laya.stage.addChild(textureImg);
			textureImg.autoSize = true; //对于加载纹理添加事件 需要设置该属性或者宽高才有效
			textureImg.on(Event.CLICK,this,onClickTexture);
			
			/*textureImg.width = 200;
			textureImg.height = 200;*/
			//Laya.loader.load(monkey0,Handler.create(this,onTextureLoaded,null,false));
			var texture:Texture = Laya.loader.getRes(monkey1);
			textureImg.graphics.drawTexture(texture);
			textureImg.mask = _shpMask;
			//Laya.stage.addChild(_shpMask); //遮罩对象不能添加到舞台
			
			Laya.stage.on(Event.CLICK,this,onClickStage);
			
		}
		
		private function onClickStage(e:Event):void
		{
			// TODO Auto Generated method stub
			trace("_shpMask=",_shpMask);
			Tween.to(_shpMask,{x:e.stageX,y:e.stageY},200);
		}
		
		private function onClickTexture(e:*):void
		{
			trace("onClickTexture..",e);
			var url:String = getClickUrl();
			Laya.loader.load(url,Handler.create(this,onTextureLoaded,null,false));
		}
		
		//load方法传的是字符串 所有参数是加载完成的资源
		private function onTextureLoaded(texture:Texture):void
		{
			textureImg.graphics.clear();
			textureImg.graphics.drawTexture(texture);
			var gf:GlowFilter = new GlowFilter("#ffff00",4,3,3);
			textureImg.filters = [gf];
			trace("texture",texture,textureImg.mouseEnabled);
		}
		
		private function switchImg(e:*=null):void
		{		
			var url:String = getClickUrl();
			//默认情况下 loadImage 所有加载过的都会添加都舞台 可以通过graphics.clear方法清除
			spImg.graphics.clear(); 
			spImg.loadImage(url);	
			img.skin = url;
		}
		
		private function getClickUrl():String
		{
			_flag = !_flag;
			var url:String = _flag?monkey1:monkey2;
			return url;
		}
		
	}
}